package io.renren.common.wxPay;

import com.github.wxpay.sdk.WXPayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Objects;

/**
 * @author Lee
 * @description 微信支付参数配置
 * @date 2020-02-23 15:33
 **/
@Configuration
public class WxConfig implements WXPayConfig {

    @Value("${baseConstant.wechatPay.appId}")
    private String appId;

    @Value("${baseConstant.wechatPay.mchId}")
    private String mchId;

    @Value("${baseConstant.wechatPay.mchKey}")
    private String mchKey;

    @Value("${baseConstant.wechatPay.keyPath}")
    private String keyPath;

    private byte[] certData;


    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return mchKey;
    }

    @Override
    public InputStream getCertStream() {
        if (Objects.isNull(certData)) {
            if (Objects.nonNull(keyPath)) {
                File file = new File(keyPath);
                try (InputStream certStream = new FileInputStream(file)) {
                    this.certData = new byte[certStream.available()];
                    certStream.read(this.certData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return new ByteArrayInputStream(this.certData);
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 10000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

}
